package core

import org.apache.spark.broadcast.Broadcast
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * 知识点：
 *  广播变量
 */
object core03_broadcast {


  def main(args: Array[String]): Unit = {


    val conf: SparkConf = new SparkConf()
      .setMaster("local[*]")
      .setAppName("test")

    val sc = new SparkContext(conf)


    val dataRdd: RDD[Int] = sc.parallelize(List(1, 2, 3, 4, 5))


    val dict: Map[Int, String] = Map((1, "zs"), (2, "ls"), (3, "ww"))

    val bcMap: Broadcast[Map[Int, String]] = sc.broadcast(dict)

    dataRdd.map(k => {(k, bcMap.value.getOrElse(k, "no value"))}).foreach(println)
  }

}
